﻿using System.Collections.Generic;
using AppData;

namespace ServerShared.AppData
{
    [System.Serializable]
    public partial class Moment //: ISerialize
    {
        public long userId;
        public long momentId;
        public long time;
        public string content;
        public string location;
        public string topic;
        public int status;
        public List<long> likes = new List<long>();
        public List<Comment> comments = new List<Comment>();
        public Tex[] texs;

        public Moment()
        {

        }
        public Moment(long userId, long momentId, long time, string content, string location, string topic, int status, List<long> likes, List<Comment> comments, Tex[] texs)
        {
            this.userId = userId;
            this.momentId = momentId;
            this.time = time;
            this.content = content;
            this.location = location;
            this.topic = topic;
            this.status = status;
            this.likes = likes;
            this.comments = comments;
            this.texs = texs;
        }
        public void AddLike(long fromUserId)
        {
            if (this.likes == null)
                likes = new List<long>() { fromUserId };
            else
            {
                if (!likes.Contains(fromUserId))
                    likes.Add(fromUserId);
            }
        }

        public void RemoveLike(long fromUserId)
        {
            if (this.likes != null)
                likes.Remove(fromUserId);
        }

        public void AddComment(Comment comment)
        {
            if (this.comments == null)
                comments = new List<Comment>() { comment };
            else
            {
                if (!comments.Contains(comment))
                    comments.Add(comment);
            }
        }

        public void RemoveComment(Comment comment)
        {
            if (this.comments != null)
                comments.Remove(comment);
        }

        public override string ToString()
        {
            return $"[userId:{userId},id:{momentId},time:{time},likes:{likes.Count},comments:{comments.Count}]";
        }
    }


    [System.Serializable]
    public partial class Comment //: ISerialize
    {
        public long momentUserId;
        public long momentId;
        public long commentId;
        public long userId => miniUserInfo.userId;
        public MiniUserInfo miniUserInfo;
        public string content;
        public List<long> likes = new List<long>();
        public long time;
        public List<SubComment> subComments;

        public Comment()
        {

        }
        public Comment(long momentUserId, long momentId, long commentId, MiniUserInfo miniUserInfo, string content, List<long> likes, long time, List<SubComment> subComments)
        {
            this.commentId = commentId;
            this.momentUserId = momentUserId;
            this.momentId = momentId;
            this.miniUserInfo = miniUserInfo;
            this.content = content;
            this.likes = likes == null ? new List<long>() : likes;
            this.time = time;
            this.subComments = subComments == null ? new List<SubComment>() : subComments;
        }

        public void AddLike(long fromUserId)
        {
            if (this.likes == null)
                likes = new List<long>() { fromUserId };
            else
            {
                if (!likes.Contains(fromUserId))
                    likes.Add(fromUserId);
            }
        }

        public void RemoveLike(long fromUserId)
        {
            if (this.likes != null)
                likes.Remove(fromUserId);
        }

        public void AddSubComment(SubComment subComment)
        {
            if (this.subComments == null)
                subComments = new List<SubComment>() { subComment };
            else
            {
                if (!subComments.Contains(subComment))
                    subComments.Add(subComment);
            }
        }

        public void RemoveSubComment(SubComment subComment)
        {
            if (this.subComments != null)
                subComments.Remove(subComment);
        }

        public override string ToString()
        {
            return
                $"[momentUserId:{this.momentUserId} momentId:{momentId} commentId:{this.commentId} commentUserId:{this.userId}]";
        }
    }

    [System.Serializable]
    public partial class SubComment // : ISerialize
    {
        public long fromUserId;
        public long toUserId;
        public string content;
        public long[] likes;
        public long time;
    }

    [System.Serializable]
    public partial class MiniUserInfo //: ISerialize
    {
        public long userId;
        public int gender;
        public string name;
        public string aboutMe;
        public int birthDate;
        public string locationProvince;
        public string locationCity;
        public Tex icon;

        public MiniUserInfo()
        {

        }

        public MiniUserInfo(
            long userId,
            int gender,
            string name,
            string aboutMe,
            int birthDate,
            string locationProvince,
            string locationCity,
            Tex icon)
        {
            this.userId = userId;
            this.gender = gender;
            this.name = name;
            this.aboutMe = aboutMe;
            this.birthDate = birthDate;
            this.locationProvince = locationProvince;
            this.locationCity = locationCity;
            this.icon = icon;
        }

        public override string ToString()
        {
            return
                $"[userId:{userId},gender:{gender},birthYear:{birthDate},location:{locationCity}, icon:{!Tex.IsNull(icon)}]";
        }
    }

    [System.Serializable]
    public partial class UserMoment //: ISerialize
    {
        public long userId => miniUserInfo.userId;
        public MiniUserInfo miniUserInfo;
        public Moment moment;
        public List<Comment> comments => moment != null ? moment.comments : null;

        public UserMoment()
        {

        }

        public UserMoment(MiniUserInfo userInfo, Moment m)
        {
            miniUserInfo = userInfo;
            this.moment = m;
        }


        public override string ToString()
        {
            return $"name:{miniUserInfo?.name} moment:{moment}";
        }
    }
}